<?php
/*************************************************

视频数据接口 - the
Author: Reload <17-12-25>

*************************************************/
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        $map['is_hot'] = 0;
        $map['status'] = 0;
        $platform = D('Platform')->get_all_page($map,0,9999,$order='`order` asc');
        $map = array();
        foreach($platform as $k => $v) {
            $map['platform_id'] = $v['id'];
            if($v['user_roi']) {
                $platform[$k]['sch_user_roi'] = $v['start_amount']*$v['user_roi']/100;
            }
            $plat_sch_count = D('PlatformScheme')->count($map);
            $platform[$k]['plat_sch_count'] = $plat_sch_count;

            //http换https
            $pic = $v['pic'];
            $platform[$k]['pic'] = preg_replace ('/http:\/\//', 'https://', $pic,1);

        }

        $map['is_hot'] =1;
        $map['status'] = 0;
        $platform_jx = D('Platform')->get_all_page($map,0,9999,$order='`order` asc');
        $map = array();
        foreach($platform_jx as $k => $v) {
            $map['platform_id'] = $v['id'];
            if($v['user_roi']) {
                $platform_jx[$k]['sch_user_roi'] = $v['start_amount']*$v['user_roi']/100;
            }
            $plat_sch_count = D('PlatformScheme')->count($map);
            $platform_jx[$k]['plat_sch_count'] = $plat_sch_count;

            //http换https
            $pic = $v['pic'];
            $platform_jx[$k]['pic'] = preg_replace ('/http:\/\//', 'https://', $pic,1);
        }

        if($platform or $platform_jx){
            $res['errorCode'] = 1;
            $res['msg'] = "获取成功";
            $res['recommend'] = $platform;
            $res['selected'] = $platform_jx;
        }else{
            $res['errorCode'] = 0;
            $res['msg'] = "暂无数据";
        }

        $this->ajaxReturn($res);
    }


    //首页节目推送送
    public function video_index() {

        //网贷早报
        $today_video = R('Push/get_push',array('tag'=>'m_mon_video'));
        foreach($today_video as $k=>$v){
            $video_id = $v['video_id'];
            $map['id'] = $video_id;
            $video_info = D('VideoHouse')->get_one($map);
            $host_id = $video_info['host'];
            $map = array();
            $map['id'] = $host_id;
            $host =  M('Host')->where($map)->field('name')->find();
            $today_video[$k]['host_name'] =$host['name'];
            $news_id = $video_info['news_id'];
            $map = array();
            $map['id'] = $news_id;
            $news = M('NewsHouse')->where($map)->field('content_short')->find();
           // print_r($video_info);
            $today_video[$k]['content_short'] =$news['content_short'];
            $today_video[$k]['content_short'] =$news['content_short'];
            $today_video[$k]['play_count'] =$video_info['play_amount']+$video_info['push_play'];
        }
       // 理财学堂
       $school_video = R('Push/get_push',array('tag'=>'m_school'));
        foreach($school_video as $k=>$v){
            $video_id = $v['video_id'];
            $map['id'] = $video_id;
            $video_info = D('VideoHouse')->get_one($map);
            $host_id = $video_info['host'];
            $map = array();
            $map['id'] = $host_id;
            $host =  M('Host')->where($map)->field('name')->find();
            $school_video[$k]['host_name'] =$host['name'];
            $news_id = $video_info['news_id'];
            $map = array();
            $map['id'] = $news_id;
            $news = M('NewsHouse')->where($map)->field('content_short')->find();
            $school_video[$k]['content_short'] =$news['content_short'];
            $school_video[$k]['play_count'] =$video_info['play_amount']+$video_info['push_play'];
        }
         // 理财学堂
       $wdfy_video = R('Push/get_push',array('tag'=>'m_wdfy'));
        foreach($wdfy_video as $k=>$v){
            $video_id = $v['video_id'];
            $map['id'] = $video_id;
            $video_info = D('VideoHouse')->get_one($map);
            $host_id = $video_info['host'];
            $map = array();
            $map['id'] = $host_id;
            $host =  M('Host')->where($map)->field('name')->find();
            $wdfy_video[$k]['host_name'] =$host['name'];
            $news_id = $video_info['news_id'];
            $map = array();
            $map['id'] = $news_id;
            $news = M('NewsHouse')->where($map)->field('content_short')->find();
            $wdfy_video[$k]['content_short'] =$news['content_short'];
            $wdfy_video[$k]['content_short'] =$news['content_short'];
            $wdfy_video[$k]['play_count'] =$video_info['play_amount']+$video_info['push_play'];
        }


         // 平台走访
       $ptzf_video = R('Push/get_push',array('tag'=>'m_ptzf'));
        foreach($ptzf_video as $k=>$v){
            $video_id = $v['video_id'];
            $map['id'] = $video_id;
            $video_info = D('VideoHouse')->get_one($map);
            $host_id = $video_info['host'];
            $map = array();
            $map['id'] = $host_id;
            $host =  M('Host')->where($map)->field('name')->find();
            $ptzf_video[$k]['host_name'] =$host['name'];
            $news_id = $video_info['news_id'];
            $map = array();
            $map['id'] = $news_id;
            $news = M('NewsHouse')->where($map)->field('content_short')->find();
            $ptzf_video[$k]['content_short'] =$news['content_short'];
            $ptzf_video[$k]['content_short'] =$news['content_short'];
            $ptzf_video[$k]['play_count'] =$video_info['play_amount']+$video_info['push_play'];
        }

        if($today_video && $school_video && $wdfy_video && $ptzf_video){
            $res = array('errorCode'=>1,'data'=>array('today_video'=>$today_video,'school_video'=>$school_video,'wdfy_video'=>$wdfy_video,'ptzf_video'=>$ptzf_video));
        }else{
            $res['errorCode'] = 0;
            $res['msg'] = "暂无数据";
        }

        $this->ajaxReturn($res);

    }

    //获取首页投顾
    public function recom_index() {
        //投顾
        $userid = I('post.userid',0,int);
        $map['id'] = $userid;
        $user = D('User')->get_one($map);
        if($user){
            $map = array();
            $map['user_id'] = $user['id'];
            $fans_info =  M('AdviserFollow')->where($map)->field('recommend_id')->select();
            if($fans_info){
                foreach($fans_info as $k=>$v){
                    $fans_list[]=$v['recommend_id'];
                }
            }
        }

        $map = array();
        $map['is_recom'] = 1;
        $recommend_list = D('Recommend')->get_all($map,$order="add_time asc");
        foreach ($recommend_list as $k => $v) {
            $user_id = $v['user_id'];
            $map = array();
            $map['id'] = $user_id;
            $userinfo = D('User')->get_one($map);
            $recommend_list[$k]['nickname'] = $userinfo['nickname'];
            $recommend_list[$k]['url_img'] = $userinfo['url_img'];
            if(in_array($v['id'],$fans_list)){
                $recommend_list[$k]['is_fans'] = 1;
            }else{
                $recommend_list[$k]['is_fans'] = 0;
            }
        }
        if($recommend_list){
            $data = array('errorCode'=>1,'msg'=>"获取成功！",'data'=>$recommend_list);
        }else{
            $data = array('errorCode'=>0,'msg'=>"获取失败！",'data'=>'');
        }
        $this->ajaxReturn($data);
    }


    //头部信息
    public function head_info(){
        $user_id = I('post.userid');
      //  print_r($user_id);die;
        $map['id'] = $user_id;
        $user = D('User')->get_one($map);
        if($user){
            //根据身份证号，自动返回性别  
            $sexint = (int)substr($user['idcard'],16,1);  
            if($sexint % 2 === 0){
                $user['sex']="女士";
            }else{
                $user['sex']="先生";

            }  

            if($user['realname']){
                $user['firstname'] =mb_substr($user['realname'],0,1,'utf8').$user['sex'];  
            }
            
        }
        
          //计算当前时间
        $now = time();
        $today = date('H',time());
        if($today>=6 and $today<=12){
            $now_time = "早上好！";
        }else if($today>12 and $today<=14){
            $now_time = "中午好！";
        }else if($today>14 and $today<18){
            $now_time = "下午好！";
        }else{
            $now_time = "晚上好！";
        }
    
        if($user && $now_time){
            $res['errorCode'] = 1;
            $res['msg'] = "获取成功";
            $res['user'] = $user['firstname'];
            $res['now_time'] = $now_time;
        }else{
            $res['errorCode'] = 0;
            $res['msg'] = "暂无数据";
        }

        $this->ajaxReturn($res);

    }
}